<?php
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Tools_Uploader');
apf_require_class('Bll_Attendance_Role');
class Oa_Attendance_CorpTripController extends Oa_Attendance_AbstractController {

    public function handle_request() {
        $error = array();
        $params = $this->request->get_parameters();
        $user_id = $_SESSION['user_id'];
        $bll_role = new Bll_Attendance_Role();
        $audit_user_ids = $bll_role->get_sup_id($user_id, array(
                1,
                2
        ));
        $audit_users = $bll_role->get_sup_name($audit_user_ids);
        $data = array();
        if($this->is_post_request()) {
            $data = array();
            $img = '';
            /*if($_FILES['file']['tmp_name']) {
                $upDir = 'upload/attendance';
                $uploader = new Tools_Uploader('file');
                $uploader->setValidExt(array(
                        '.jpg',
                        '.gif'
                ));
                $uploader->setUpDir(CODE_PATH . $upDir); // 需修改为你自己的路径
                if($uploader->upload()) {
                    $img = $uploader->getFileName();
                } else {
                    $error[] = $uploader->getErrorMsg();
                    echo "<script type='text/javascript'>alert('"
                        . $uploader->getErrorMsg()
                        . "')</script>";
                }
            }*/
            if(!empty($params['img_file'])){
                if(strpos($params['img_file'],"attendance")){
                    $img = str_replace("/upload/attendance/","",$params['img_file']);
                }
            }
            apf_require_class('Bll_Attendance_Apply');
            $bll_apply = new Bll_Attendance_Apply();
            $now = time();
            $start_last = intval(substr($params['start_time'],0,2));
            $end_last = intval(substr($params['end_time'],0,2));
            $params['days'] = $end_last - $start_last;

            $data = array(
                    'user_id' => $user_id,
                    'apply_time' => date("Y-m-d H:i:s", $now),
                    'apply_time_ext' => $now,
                    'start_time' => $params['app_date'].' '.$params['start_time'],
                    'end_time' => $params['app_date'].' '.$params['end_time'],
                    'leave_time' => $params['days'],
                    'type' => '公出',
                    'leave_reason' => '公出',
                    'leave_desc' => $params['desc'],
                    'img'=>$img,
                    'status' => '未审核',
                    'r_user_id' => $audit_user_ids[1],
                    //'b_user_id' => $audit_user_ids[2],
                    'last_touch' => date("Y-m-d H:i:s")
            );
            if (!$error) {
                $bll_apply->apply($data);
            }
        }
        $fd = isset($params['fd']) ? $params['fd'] : '';
        $fd_s = isset($params['s']) ? $params['s'] : '';
        $fd_e = isset($params['e']) ? $params['e'] : '';
        if((strlen($fd_s) != strlen($fd_e) || strlen($fd_s) !=5) || ( strpos($fd_s, ':')==0 || strpos($fd_e, ':')==0 ) ||(intval(date('Hi',strtotime($fd_s))) >= intval(date('Hi',strtotime($fd_e))))){
            $fd_s = '09:00';
            $fd_e = '18:00';
        }
        $this->request->set_attribute('fd',$fd);
        $this->request->set_attribute('fd_s',$fd_s);
        $this->request->set_attribute('fd_e',$fd_e);
        $this->request->set_attribute('audit_users', $audit_users);
        return 'Oa_Attendance_CorpTrip';
    }
}
